﻿using System;
using System.Drawing;
using System.Windows.Forms;
using System.Xml;

namespace TextEditor
{
   

    public class ZYTextKeyWord : ZYTextElement
    {
        private string strErrorMessage = null;

        public override bool FromXML(XmlElement myElement)
        {
            if (base.FromXML(myElement))
            {
                this.GetTemplateRecord();
                return true;
            }
            return false;
        }

        public ET_Document GetTemplateRecord()
        {
            this.strErrorMessage = null;
            KB_Item templateByID = ZYKBBuffer.Instance.GetTemplateByID(this.Source);
            if (templateByID == null)
            {
                this.strErrorMessage = "未找到编号为[ " + this.Source.ToString() + " ]的知识项目";
            }
            else
            {
                this.Text = templateByID.ItemText;
                ET_Document myRecord = new ET_Document();
                myRecord.SetKeyWord(templateByID.ItemValue);
                if (base.myOwnerDocument.DataSource.DBConn.TestRecordExist(myRecord))
                {
                    return myRecord;
                }
                this.strErrorMessage = "未找到编号为[ " + templateByID.ItemValue + " ]的模板";
            }
            return null;
        }

        public override string GetXMLName()
        {
            return "keyword";
        }

        public override bool HandleClick(int x, int y, MouseButtons Button)
        {
            if ((!this.Deleteted && this.myBounds.Contains(x, y)) && !base.myParent.Locked)
            {
                if (this.GetTemplateRecord() != null)
                {
                    KB_Item templateByID = ZYKBBuffer.Instance.GetTemplateByID(this.Source);
                    base.myOwnerDocument.BeginContentChangeLog();
                    base.myOwnerDocument.BeginUpdate();
                    if (base.myOwnerDocument._InsertDocument(templateByID.ItemValue))
                    {
                        base.myOwnerDocument.Content.CurrentElement = this;
                        base.myOwnerDocument.Content.DeleteCurrentElement();
                    }
                    base.myOwnerDocument.EndContentChangeLog();
                    base.myOwnerDocument.EndUpdate();
                    return true;
                }
                MessageBox.Show(base.myOwnerDocument.OwnerControl, this.strErrorMessage, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            return false;
        }

        public override void HandleEnter()
        {
            base.myOwnerDocument.RefreshElement(this);
        }

        public override void HandleLeave()
        {
            base.myOwnerDocument.RefreshElement(this);
        }

        public override bool HandleMouseMove(int x, int y, MouseButtons Button)
        {
            if (this.Bounds.Contains(x, y))
            {
                base.myOwnerDocument.SetCursor(Cursors.Arrow);
                base.myOwnerDocument.SetToolTip("单击引用模板 [ " + this.Text + " ] " + ((this.strErrorMessage == null) ? "" : ("\r\n" + this.strErrorMessage)), 4, base.myBounds);
                base.myOwnerDocument.CurrentHoverElement = this;
                return true;
            }
            return false;
        }

        public override bool isTextElement()
        {
            return true;
        }

        public override bool RefreshSize()
        {
            SizeF ef = base.myOwnerDocument.View.MeasureString(this.Text, null);
            base.Width = ((int) ef.Width) + 2;
            if (base.Width < 20)
            {
                base.Width = 20;
            }
            this.Height = base.myOwnerDocument.View.DefaultRowHeight();
            return true;
        }

        public override bool RefreshView()
        {
            int realLeft = this.RealLeft;
            int realTop = this.RealTop;
            if (!base.myOwnerDocument.Info.Printing && (base.myOwnerDocument.Content.isCurrentElement(this) || (base.myOwnerDocument.CurrentHoverElement == this)))
            {
                base.myOwnerDocument.View.FillRectangle((this.strErrorMessage == null) ? Color.Thistle : Color.Tomato, this.Bounds);
                base.myOwnerDocument.View.DrawSingleLine2(this.Text, Color.Red, realLeft, realTop, this.Width);
            }
            else
            {
                base.myOwnerDocument.View.DrawLine(SystemColors.Highlight, this.myBounds.Left, this.myBounds.Bottom - 1, this.myBounds.Right, this.myBounds.Bottom - 1);
                Color highlightText = SystemColors.HighlightText;
                if (base.myParent.Locked)
                {
                    highlightText = ZYTextConst.LockedForeColor;
                }
                else
                {
                    highlightText = SystemColors.WindowText;
                }
                base.myOwnerDocument.View.DrawSingleLine2(this.Text, highlightText, realLeft, realTop, this.Width);
            }
            return true;
        }

        public override string ToZYString()
        {
            return this.Text;
        }

        public int Source
        {
            get
            {
                return base.myAttributes.GetInt32("source");
            }
            set
            {
                base.myAttributes.SetValue("source", value);
            }
        }

        public string Text
        {
            get
            {
                return base.myAttributes.GetString("text");
            }
            set
            {
                base.myAttributes.SetValue("text", value);
            }
        }
    }
}

